package ch.ethz.fcl.metrobuzz.controller.gui.twl;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.tools.MBTools;

import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;

public class ScalableRenderer extends LWJGLRenderer {
	int width;
	int height;

	public ScalableRenderer(int width, int height) throws LWJGLException {
		super();

		this.width = width;
		this.height = height;
		this.setViewport(0, 0, width, height);
	}

	@Override
	public void setupGLState() {
		GL11.glPushAttrib(GL11.GL_ENABLE_BIT | GL11.GL_TRANSFORM_BIT
				| GL11.GL_HINT_BIT | GL11.GL_COLOR_BUFFER_BIT
				| GL11.GL_SCISSOR_BIT | GL11.GL_LINE_BIT | GL11.GL_TEXTURE_BIT);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glPushMatrix();
		GL11.glLoadIdentity();
		GL11.glOrtho(0, width, height, 0, -1.0, 1.0);

		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		GL11.glPushMatrix();
		GL11.glLoadIdentity();

		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glEnable(GL11.GL_LINE_SMOOTH);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glDisable(GL11.GL_SCISSOR_TEST);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);

		GL11.glLineWidth(3.0f);
		GL11.glPointSize(10);
		GL11.glBegin(GL11.GL_POINTS);
		GL11.glColor3f(0, 1, 0);
		GL11.glVertex2d(0, 0);
		GL11.glColor3f(1, 0, 0);
		GL11.glVertex2d(this.getViewportX(), this.getViewportY());
		GL11.glColor3f(0, 0, 1);
		GL11.glVertex2d(this.getViewportX() + this.getWidth(),
				this.getViewportY() + this.getHeight());
		GL11.glEnd();
		MBTools.debug("x " + this.getViewportX() + " y " + this.getViewportY()
				+ " width " + this.getWidth() + " height " + this.getHeight(),
				true);
		// this.hasScissor = false;
	}

	@Override
	public void syncViewportSize() {
		// We don't ever change the viewport size. It's always the size defined
		// in the constructor.
	}
}
